package edu.sctu;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 1 on 2018/6/3.
 */
public class Database {
    public List<User> userList(){
        List<User> userList=new ArrayList<>();
        //3+1读取数据库数据保存到List中
        Connection connection=null;
        Statement statement=null;
        ResultSet resultSet=null;

        String sql="select * from t_users";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/sctu_javaweb_2018?serverTimezone=UTC",
                    "root",
                    "123456"
            );
            statement=connection.createStatement();
            resultSet=statement.executeQuery(sql);
            while (resultSet.next()){
                int id=resultSet.getInt(1);
                String username=resultSet.getNString(2);
                String password=resultSet.getNString(3);

                User user=new User();
                user.setId(id);
                user.setUsername(username);
                user.setPassword(password);

                userList.add(user);
            }

            resultSet.close();
            statement.close();
            connection.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }
    public void addUser(){

    }
    public void deleteUser(){

    }
    public void updateUser(){

    }
}
